"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 兼容M* create function
Case Name   : create function returns返回类型为函数主体返回类型可转换类型
Create At   : 2023.03.15
@zou_jialiang0501129488
Description :
    1、创建兼容b库并查询插件是否加载成功
    2、create function returns返回类型为函数主体返回类型可转换类型
    3、清理环境
Expect      :
    1、成功，插件查询成功
    2、成功
    3、清理成功
History     :
"""

import os
import unittest

from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest01(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_create_function_0015'
        self.sql_name = 'sql_file_0015.sql'

    def test_create_function(self):
        text = '-----step1:创建兼容b库库并查询插件;expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility ='B';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS,
                      sql_res, '兼容B库创建失败')

        sql_cmd1 = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd1)
        sql_res1 = self.sh_primary.execut_db_sql(sql_cmd1, dbname=self.db_name)
        self.log.info(sql_res1)
        self.assertIn('dolphin', sql_res1, '查询失败')

        text = '-----step2:create function returns返回类型为函数主体' \
               '返回类型可转换类型;expect:成功-----'
        self.log.info(text)
        sql_cmd2 = f"""--returns char return int;expect:成功
            drop function if exists f_create_function_0015;
            create function f_create_function_0015(s int)
            returns char(50)
            no sql
            return s;
            --returns char return decimal;expect:成功
            drop function if exists f_create_function_0015_1;
            create function f_create_function_0015_1(s decimal(7,3))
            returns char(50)
            no sql
            return s;
            --returns int return decimal;expect:成功
            drop function if exists f_create_function_0015_2;
            create function f_create_function_0015_2(s decimal(7,3))
            returns int
            no sql
            return s;
            --returns decimal return int;expect:成功
            drop function if exists f_create_function_0015_3;
            create function f_create_function_0015_3(s int)
            returns decimal(7,3)
            no sql
            return s;"""
        self.log.info(sql_cmd2)
        echo_cmd = f"rm -rf {self.sql_name};" \
                   f"echo \"\"\"{sql_cmd2}\"\"\" > {self.sql_name};" \
                   f"ls | grep {self.sql_name}"
        self.log.info(echo_cmd)
        res = self.common.get_sh_result(self.sh_primary.node, echo_cmd)
        self.assertIn(self.sql_name, res, '文件写入失败')

        sql_cmd3 = f'source {macro.DB_ENV_PATH};gsql -d {self.db_name} ' \
                   f'-p {self.sh_primary.node.db_port} ' \
                   f'-f {self.sql_name}'
        self.log.info(sql_cmd3)
        sql_res3 = self.common.get_sh_result(self.sh_primary.node, sql_cmd3)
        self.log.info(sql_res3)
        self.assertEqual(4, sql_res3.count('CREATE FUNCTION'), '函数创建失败')

        sql_cmd = f'select f_create_function_0015(20);' \
                  f'select f_create_function_0015_1(20.56);' \
                  f'select f_create_function_0015_2(20.567);' \
                  f'select f_create_function_0015_3(30);'
        self.log.info(sql_cmd)
        res = self.sh_primary.execut_db_sql(sql_cmd, dbname=self.db_name)
        msg_lis = ['20\n(1 row)', '20.56\n(1 row)',
                   '21\n(1 row)', '30\n(1 row)']

        for msg in msg_lis:
            self.assertIn(msg, res, '函数调用失败')

    def tearDown(self):
        text = '-----step3:清理环境;expect:成功-----'
        self.log.info(text)
        sql_cmd4 = f"drop database if exists {self.db_name};"
        self.log.info(sql_cmd4)
        sql_res4 = self.sh_primary.execut_db_sql(sql_cmd4)
        self.log.info(sql_res4)
        del_cmd = f'rm -rf {self.sql_name};' \
                  f'if [ -e {self.sql_name} ]; ' \
                  f'then echo "exists"; else echo "not exists"; fi'
        self.log.info(del_cmd)
        del_res = self.common.get_sh_result(self.sh_primary.node, del_cmd)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS,
                      sql_res4, '清理环境失败')
        self.assertIn('not exists', del_res, '文件删除失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
